package com.link2room.rodimus.util;

import com.link2room.common.util.LogUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 *
 * Created by LN on 2017/10/23
 */
public class LotteryUtil {
    private static Logger logger = LoggerFactory.getLogger(LogUtil.class);
    /**
     * now 是否在startTime endTime区间内.  闭区间
     * @param startTime
     * @param endTime
     * @param now
     * @return
     */
    public static boolean isInInterval(LocalTime startTime , LocalTime endTime , LocalTime now){
        boolean isInTime;
        if(endTime.compareTo(startTime) == 0){
            isInTime = true;
        }else if(endTime.isBefore(startTime)){
            isInTime = now.isAfter(startTime) || now.isBefore(endTime);
        }else {
            isInTime = now.isAfter(startTime) && now.isBefore(endTime);
        }
        return isInTime;
    }
    /**
     * 定义一个连续集合
     * 集合中元素x满足:(minElement,maxElement]
     * 数学表达式为：minElement < x <= maxElement
     *
     */
    private class ContinuousList {

        private final double minElement;
        private final double maxElement;
        private final String code;
        ContinuousList(String code,double minElement, double maxElement){
            if(minElement > maxElement){
                throw new IllegalArgumentException("区间不合理，minElement不能大于maxElement！");
            }
            this.minElement = minElement;
            this.maxElement = maxElement;
            this.code = code;
        }

        /**
         * 判断当前集合是否包含特定元素
         * @param element
         * @return
         */
        boolean isContainKey(double element){
            boolean flag = false;
            if(element > minElement && element <= maxElement){
                flag = true;
            }
            return flag;
        }

        public String getCode() {
            return code;
        }
    }
    public static class DrawRate{
        public DrawRate(String code, Double d) {
            this.code = code;
            this.d = d;
        }

        /**
         * 代码
         */
        private String code ;
        /**
         * 概率值
         */
        private Double d;

        public String getCode() {
            return code;
        }

        public Double getD() {
            return d;
        }
    }
    private List<ContinuousList> lotteryList;   //概率连续集合
    private double maxElement;                  //这里只需要最大值，最小值默认为0.0
    private Random r = new Random();            //随机数生成器
    /**
     * 构造抽奖集合
     * @param list 为奖品的概率
     */
    public LotteryUtil(List<DrawRate> list){
        logger.debug("LotteryUtil 初始化");
        lotteryList = new ArrayList<>();
        if(list.size() == 0){
            throw new IllegalArgumentException("没有奖项！");
        }
        double minElement = 0d;
        ContinuousList continuousList = null;

        for(DrawRate d : list){
            minElement = maxElement;
            maxElement = maxElement + d.getD();
            continuousList = new ContinuousList(d.getCode(),minElement, maxElement);
            lotteryList.add(continuousList);
        }
        logger.debug("LotteryUtil 初始化结束");
    }
    /**
     * 进行抽奖操作
     * 返回：奖品的概率list集合中的下标
     */
    public String randomColunmCode(){
        double d = r.nextDouble() * maxElement;  //生成0-1间的随机数

//        int size = lotteryList.size();
        double finalD = d;
        ContinuousList res = lotteryList.stream()
                .filter(cl -> cl.isContainKey(finalD))
                .findAny()
                .orElseThrow(()->new IllegalArgumentException("概率设置不合理"));
     /*   for(int i = 0; i < size; i++){
            ContinuousList cl = lotteryList.get(i);
            if(cl.isContainKey(d)){
                index = i;
                break;
            }
        }
       */
        return res.getCode();

    }

    public double getMaxElement() {
        return maxElement;
    }

}
